In [1]:
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Flatten, Dropout
from keras.layers import Conv2D, MaxPooling2D
from keras.utils import to_categorical
from keras.losses import categorical_crossentropy
from keras.optimizers import Adam
import keras.backend as K
In [2]:
train_da, test_da = mnist.load_data()
x_train, y_train = train_da
x_test, y_test = test_da
In [3]:
from IPython.display import Image
from PIL import Image
import numpy as np
import imageio
#im = imageio.imread('./dig5.png')
#x_train[0]=im
a = np.concatenate(x_train[0:4], axis=1)
print(" ".join([str(x) for x in y_train[0:4]]))
for i in range(3):
a = np.concatenate([a, np.concatenate(x_train[4+i*4:8+i*4], axis=1)], axis=0)
print(" ".join([str(x) for x in y_train[4+i*4:8+i*4]]))
img = Image.fromarray(a, 'L')
img.resize((896, 896), Image.NEAREST)
Out[3]:
In [4]:
data_format = K.image_data_format()
rows, cols = 28, 28
num_classes = 10
train_size = x_train.shape[0]
test_size = x_test.shape[0]
if data_format == 'channels_first':
x_train = x_train.reshape(train_size, 1, rows, cols)
x_test = x_test.reshape(test_size, 1, rows, cols)
input_shape = (1, rows, cols)
else:
x_train = x_train.reshape(train_size, rows, cols, 1)
x_test = x_test.reshape(test_size, rows, cols, 1)
input_shape = (rows, cols, 1)
# norm data to float in range 0..1
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
# convert class vectors to one hot vector
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
In [5]:
x_train = x_train[:100]
y_train = y_train[:100]
In [6]:
model = Sequential()
model.add(Flatten(input_shape=input_shape))
model.add(Dense(num_classes, activation='softmax'))
print(model.summary())
In [7]:
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
SVG(model_to_dot(model, show_shapes=True).create(prog='dot', format='svg'))
Out[7]:
In [8]:
model.compile(loss=categorical_crossentropy,
optimizer=Adam(),
metrics=['accuracy'])
In [9]:
batch_size = 128
epochs = 20
In [10]:
from keras.callbacks import ModelCheckpoint, Callback
history = model.fit(
x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test),
callbacks=[ModelCheckpoint("myWeights.hdf5", monitor='val_loss', save_best_only=True, mode='auto', period=1)]
)
#history = model.fit(
# x_train, y_train,
# batch_size=batch_size,
# epochs=epochs,
# verbose=1,
# validation_data=(x_test, y_test)
#)
In [11]:
from matplotlib import pyplot as plt
# utility to display a row of digits with their predictions
def display_digits(digits, predictions, labels, title, n):
plt.figure(figsize=(13,3))
digits = np.reshape(digits, [n, 28, 28])
digits = np.swapaxes(digits, 0, 1)
digits = np.reshape(digits, [28, 28*n])
plt.yticks([])
plt.xticks([28*x+14 for x in range(n)], predictions)
for i,t in enumerate(plt.gca().xaxis.get_ticklabels()):
if predictions[i] != labels[i]: t.set_color('red') # bad predictions in red
plt.imshow(digits)
plt.grid(None)
plt.title(title)
score = model.evaluate(x_test, y_test, verbose=0)
print('Test1 loss:', score[0])
print('Test1 accuracy:', score[1])
im = imageio.imread('./dig5.png')
pix = np.empty([28, 28, 1])
for i in range(0,28):
for j in range(0,28):
pix[i][j][0]=0.0 # im[i][j]/256
x_test[0]=pix
x_test[1]=pix
x_test[2]=pix
score = model.evaluate(x_test, y_test, verbose=1)
print('Test2 loss:', score[0])
print('Test2 accuracy:', score[1])
# recognize digits from local fonts
pixim = np.empty([1,28,28,1])
for i in range(0,28):
for j in range(0,28):
pixim[0][i][j][0] = im[i][j]/256
N=1
font_labels = [5]
probabilities = model.predict(pixim, steps=1)
predicted_labels = np.argmax(probabilities, axis=1)
print(predicted_labels)
display_digits(pixim, predicted_labels, font_labels, "predictions from local fonts (bad predictions in red)", N)
In [12]:
from pandas import DataFrame
df_loss = DataFrame(data={
'Epoche': history.epoch * 2,
'Legende': ['Loss auf Trainingsdaten'] * len(history.epoch) + ['Loss auf Testdaten'] * len(history.epoch),
'Loss': history.history['loss'] + history.history['val_loss']
})
df_accuracy = DataFrame(data={
'Epoche': history.epoch * 2,
'Legende': ['Accuracy auf Trainingsdaten'] * len(history.epoch) + ['Accuracy auf Testdaten'] * len(history.epoch),
# 'Accuracy': history.history['acc'] + history.history['val_acc']
'Accuracy': history.history['accuracy'] + history.history['val_accuracy']
})
print(df_loss)
print(df_accuracy)
In [13]:
import altair as alt
alt.renderers.enable('notebook')
chart_loss = alt.Chart(df_loss).mark_line().encode(
x='Epoche', y='Loss', color='Legende')
chart_accuracy = alt.Chart(df_accuracy).mark_line().encode(
x='Epoche', y='Accuracy', color='Legende')
chart = chart_loss + chart_accuracy
chart.resolve_scale(y='independent')
Out[13]:
In [14]:
model = Sequential()
model.add(Flatten(input_shape=input_shape))
model.add(Dense(400, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
print(model.summary())
SVG(model_to_dot(model, show_shapes=True).create(prog='dot', format='svg'))
Out[14]:
In [15]:
model.compile(loss=categorical_crossentropy,
optimizer=Adam(),
metrics=['accuracy'])
history = model.fit(
x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test)
)
In [16]:
def evaluate(model, history):
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
df_loss = DataFrame(data={
'Epoche': history.epoch * 2,
'Legende': ['Loss auf Trainingsdaten'] * len(history.epoch) + ['Loss auf Testdaten'] * len(history.epoch),
'Loss': history.history['loss'] + history.history['val_loss']
})
df_accuracy = DataFrame(data={
'Epoche': history.epoch * 2,
'Legende': ['Accuracy auf Trainingsdaten'] * len(history.epoch) + ['Accuracy auf Testdaten'] * len(history.epoch),
# 'Accuracy': history.history['acc'] + history.history['val_acc']
'Accuracy': history.history['accuracy'] + history.history['val_accuracy']
})
chart_loss = alt.Chart(df_loss).mark_line().encode(
x='Epoche', y='Loss', color='Legende')
chart_accuracy = alt.Chart(df_accuracy).mark_line().encode(
x='Epoche', y='Accuracy', color='Legende')
chart = chart_loss + chart_accuracy
return chart.resolve_scale(y='independent')
In [17]:
evaluate(model, history)
Out[17]:
In [18]:
(x_train, y_train), (x_test, y_test) = mnist.load_data()
train_size = x_train.shape[0]
test_size = x_test.shape[0]
data_format = K.image_data_format()
if data_format == 'channels_first':
x_train = x_train.reshape(train_size, 1, rows, cols)
x_test = x_test.reshape(test_size, 1, rows, cols)
input_shape = (1, rows, cols)
else:
x_train = x_train.reshape(train_size, rows, cols, 1)
x_test = x_test.reshape(test_size, rows, cols, 1)
input_shape = (rows, cols, 1)
# norm data to float in range 0..1
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
# convert class vectors to one hot vector
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
In [19]:
model = Sequential()
model.add(Flatten(input_shape=input_shape))
model.add(Dense(400, activation='relu'))
model.add(Dropout(rate=0.5))
model.add(Dense(num_classes, activation='softmax'))
print(model.summary())
SVG(model_to_dot(model, show_shapes=True).create(prog='dot', format='svg'))
Out[19]:
In [20]:
batch_size = 128
epochs = 12
model.compile(loss=categorical_crossentropy,
optimizer=Adam(),
metrics=['accuracy'])
history = model.fit(
x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test)
)
In [21]:
evaluate(model, history)
Out[21]:
In [22]:
model = Sequential()
model.add(Conv2D(
32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv2D(
64, kernel_size=(3, 3),
activation='relu'))
model.add(MaxPooling2D(
pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(200, activation='relu'))
model.add(Dropout(rate=0.5))
model.add(Dense(num_classes, activation='softmax'))
print(model.summary())
SVG(model_to_dot(model, show_shapes=True).create(prog='dot', format='svg'))
Out[22]:
In [23]:
batch_size = 128
epochs = 12
model.compile(loss=categorical_crossentropy,
optimizer=Adam(),
metrics=['accuracy'])
history = model.fit(
x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test)
)
In [24]:
evaluate(model, history)
N=1
font_labels = [5]
probabilities = model.predict(pixim, steps=1)
predicted_labels = np.argmax(probabilities, axis=1)
print(predicted_labels)
print(probabilities)
display_digits(pixim, predicted_labels, font_labels, "predictions from local fonts (bad predictions in red)", N)
Schöne 3D-Visualisierung eines Convolutional Networks für MNIST: http://scs.ryerson.ca/~aharley/vis/conv/
In [ ]: